﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UCL.Powersource.Analysis;
using UCL.Powersource.Helpers;
using UCL.Powersource.Statistics;
using UCL.Powersource.ViewModels.Analysis;

namespace UCL.Powersource.ViewModels.AnalysisBase
{
    public abstract class ManyTeamsAtDifferentTimesStatisticsViewModelBase : StatisticViewModelBase
    {
        public ManyTeamsAtDifferentTimesStatisticsViewModelBase()
            : base(StatisticViewModelTypes.MultipleTeams_MultipleTimeFrames, false, false, false)
        { }

        protected override void Recalculate_Internal()
        {
            StatisticResultsTable_Recalculate();
        }

        protected override bool OnActivating()
        {
            StatisticResultsTable_Recalculate();
            return true;
        }

        protected override bool OnDeactivating()
        {
            return true;
        }

        public override bool CanRegisterStatistic(IStatistic statistic)
        {
            return true;
        }

        protected override void OnActiveStatisticChanged()
        {
            base.OnActiveStatisticChanged();
            StatisticResultsTable_Recalculate();
        }

        protected override void OnTeamProjectConfigurationsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            base.OnTeamProjectConfigurationsCollectionChanged(sender, e);
            StatisticResultsTable_Recalculate();
        }

        protected override void OnTimeFramesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            base.OnTimeFramesCollectionChanged(sender, e);
            StatisticResultsTable_Recalculate();
        }

        protected virtual void StatisticResultsTable_Recalculate()
        {
            if (ActiveIReportingContext == null ||
                ActiveIReportingContext.ActiveStatistic == null ||
                ActiveIReportingContext.SelectedITimeFrames == null ||
                ActiveIReportingContext.SelectedITfsTeamProjectConfigurations == null)
            {
                StatisticResultsTable = new DataTable();
                return;
            }

            StatisticResultsTable = DataTableBuilder.GetTableForActiveStatistic(ActiveIReportingContext.ActiveStatistic,
                ActiveIReportingContext.SelectedITfsTeamProjectConfigurations, ActiveIReportingContext.SelectedITimeFrames);
        }
    }
}
